<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-user" v-model="name"></el-input>
            <el-button class="mr-l-5" type="primary" @click="load">查询</el-button>
            <el-button class="mr-l-5" type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-button type="primary" @click="handleAdd()">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-button type="danger" @click="delBath">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </div>
        <el-table :data="tableData"  stripe border header-cell-class-name="headerBg"
                  @selection-change="handleSelectionChange"
                  row-key="id"
                  default-expand-all
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="Id" width="100px"></el-table-column>
            <el-table-column prop="name" label="名称" ></el-table-column>
            <el-table-column prop="path" label="路径" ></el-table-column>
            <el-table-column prop="pagePath" label="页面路径" ></el-table-column>
            <el-table-column prop="icon" label="图标" align="center" class-name="fontsize1" label-class-name="fontsize2">
              <template slot-scope="scope">
                  <i :class="scope.row.icon" />
              </template>
            </el-table-column>
            <el-table-column prop="description" label="描述" >
            </el-table-column>
            <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                  <el-button size="mini" type="primary" @click="handleAdd(scope.row.id)"v-if=" !scope.row.pid && !scope.row.path" >新增子菜单<i class="el-icon-add-location"></i></el-button>
                    <el-button
                            size="mini"
                            type="success"
                            @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
                    <el-popconfirm
                            confirm-button-text='好的'
                            cancel-button-text='不用了'
                            icon="el-icon-info"
                            icon-color="red"
                            title="这是一段内容确定删除吗？"
                            @confirm="handleDelete( scope.row.id)"
                    >
                        <el-button
                                size="mini"
                                type="danger" slot="reference" class="mr-l-5">删除<i class="el-icon-remove-outline"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="角色信息" :visible.sync="dialogFormVisible" width="30%" >
            <el-form label-width="80px" size="small">
                <el-form-item label="名称" ><el-input v-model="form.name" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="路径" ><el-input v-model="form.path" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="页面路径" ><el-input v-model="form.pagePath" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
                <el-form-item label="图标" >
                    <el-select v-model="form.icon" placeholder="请选择">
                      <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                        <i :class="item.value" style="margin-right: 2px"/> {{item.name}}
                      </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="描述" ><el-input v-model="form.description" autocomplete="off" placeholder="请输入密码" style="width: 80%"></el-input></el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
    export default {
        name: "User",
        data(){
            return {
                flag: false,
                tableData: [],
                name:'',
                form: {name:"",path:'',icon:'',description:"",pid:"",pagePath:""},
                dialogFormVisible: false,
                pageNum: 1,
                pageSize: 10,
                totalNum: 10,
                multipleSelection: [],
                options:[]
            }
        },
        created() {
            //请求分页查询数据
            this.load()
        },
        methods:{
            load(){
                this.request.get('/menu/getAll',{
                }).then(res => {
                    this.tableData = res.data
                })
            },
            save(){
                console.log(this.form)
                this.request.post("/menu",this.form).then(res => {
                    console.log(res)
                    if (res.status === '200'){
                        this.$message.success("操作成功")
                        this.dialogFormVisible= false;
                        this.load();
                    }else{
                        this.$message.error("操作失败")
                        this.dialogFormVisible= false;
                    }
                })
            },
            handleDelete(id){
                this.request.delete("/menu/"+id).then(res => {
                    if (res.status === '200'){
                        this.$message.success("删除成功")
                        this.pageNum = 1
                        this.load();
                    }else{
                        this.$message.error("删除失败")
                    }
                })
            },
            handleEdit(row){
                this.form = row
                this.dialogFormVisible= true
            },
            handleAdd(pid){
                this.dialogFormVisible = true
                this.form = {}
                if(pid){
                  this.form.pid = pid;
                }
                this.request.get("/menu/icons").then(res => {
                  this.options = res.data
                })
            },
            reset(){
                this.name = "",
                    this.load();
            },


            handleSelectionChange(val) {
                this.multipleSelection = val
            },
            delBath(){
                let ids = this.multipleSelection.map(v => v.id);
                this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                    cancelButtonText: '取消',
                  confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                  console.log("shanchu")
                    this.request.post("/menu/del/batch",ids).then(res => {
                        if (res){
                            this.$message.success("删除成功")
                            this.load();
                        }else{
                            this.$message.error("删除失败")
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            handleSizeChange(pageSize){
                this.pageSize = pageSize
                this.load();
            },
            handleCurrentChange(pageNum){
                this.pageNum = pageNum
                this.load()
            }
        }
    }
</script>

<style>
.headerBg{
    background: #eee !important;
}
.fontsize1{
  font-size: 18px;
}
.fontsize2{
  font-size: 12px;
}
</style>